<?php
// +----------------------------------------------------------------------
// | HkCms
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace common\controller;

use libs\system\RBAC;

//定义是后台
define('IN_ADMIN', true);

class AdminBase extends Hkcms
{
    public function _initialize()
    {
        parent::_initialize();
        config([
            "user_auth_on" => true, //是否开启权限认证
            "user_auth_type" => 1, //默认认证类型 1 登录认证 2 实时认证
            "require_auth_module" => "", //需要认证模块
            "not_auth_module" => "Login", //无需认证模块
            "user_auth_gateway" => url("admin/login/login"), //登录地址
        ]);
        if (false == RBAC::AccessDecision($this->request->module())) {
            //检查是否登录
            if (false === RBAC::checkLogin()) {
                //跳转到登录界面
                $this->redirect(config('user_auth_gateway'));
            }
            //没有操作权限
            $this->error('您没有操作此项的权限！');
        }

    }

    /**
     * 操作错误跳转的快捷方法
     * @param string $msg
     * @param null $url
     * @param string $data
     * @param int $wait
     * @param array $header
     * @return \think\response\Json|void
     */
    public function error($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
    {
        model('admin/Operationlog')->record($msg, 0);
        if ($this->request->isAjax()) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['info'=>$msg,'status'=>0,'url'=>$url,'state'=>"fail"]));
        }
        parent::error($msg, $url, $data, $wait, $header);
    }

    /**
     * 操作成功跳转的快捷方法
     * @param string $msg
     * @param null $url
     * @param string $data
     * @param int $wait
     * @param array $header
     * @return \think\response\Json|void
     */
    public function success($msg = '', $url = null, $data = '', $wait = 3, array $header = [])
    {
        model('admin/Operationlog')->record($msg, 1);
        if ($this->request->isAjax()) {
            header('Content-Type:application/json; charset=utf-8');
            exit(json_encode(['info'=>$msg,'status'=>1,'url'=>$url,'state'=>"success",'data'=>$data,'wait'=>$wait]));
        }
        parent::success($msg, $url, $data, $wait, $header);
    }
}